package pl.iachoo.ctl.tabory.ui.editors.vagons;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.ViewPart;

import pl.iachoo.ctl.tabory.dao.hibernate.interfaces.IVagonService;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.VagonVO;
import pl.iachoo.ctl.tabory.ui.editors.vagons.composites.VagonDetailSectionPart;
import pl.iachoo.ctl.tabory.ui.editors.vagons.composites.VagonTableSectionPart;
import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;

public class VagonEditor extends ViewPart {

	public static final String ID = "pl.iachoo.ctl.tabory.ui.editors.vagons.VagonEditor";

	private final static Logger log = Logger.getLogger(VagonEditor.class);

	private VagonSelection selection;
	private final FormToolkit formToolkit = new FormToolkit(
			Display.getDefault());

	private VagonDetailSectionPart vagonDetailSectionPart;

	private VagonTableSectionPart vagonTableSectionPart;

	public VagonEditor() {
		selection = new VagonSelection();
		IVagonService vagonService = ServiceManager.getVagonService();
		try {
			List<VagonVO> listVagons = vagonService.getAllVagons();
			selection.setListVagons(listVagons);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		Form form = formToolkit.createForm(parent);
		formToolkit.paintBordersFor(form);
		form.getBody().setLayout(new GridLayout(2, false));

		vagonTableSectionPart = new VagonTableSectionPart(form.getBody(),
				formToolkit, Messages.getString("vagons.editor.table.title"),
				true, true);
		Section section = vagonTableSectionPart.getSection();
		section.setExpanded(true);
		section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1,
				1));
		formToolkit.paintBordersFor(section);
		vagonTableSectionPart.setSelection(selection);

		vagonDetailSectionPart = new VagonDetailSectionPart(form.getBody(),
				formToolkit, Section.TITLE_BAR);
		Section sctnNewSectionpart = vagonDetailSectionPart.getSection();
		sctnNewSectionpart.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true,
				false, 1, 1));
		formToolkit.paintBordersFor(sctnNewSectionpart);

		vagonDetailSectionPart.setSelection(selection);

		vagonTableSectionPart
				.addSelectionListener(new ISelectionListener<VagonSelection>() {

					@Override
					public void selectionChanged(VagonSelection selection) {
						log.debug("Setting selection to Vagon Details Section.");
						vagonDetailSectionPart.setSelection(selection);
					}
				});

		vagonDetailSectionPart
				.addSelectionListener(new ISelectionListener<VagonSelection>() {

					@Override
					public void selectionChanged(VagonSelection selection) {
						log.debug("Setting selection to Vagon Table Section.");
						vagonTableSectionPart.setSelection(selection);
						log.debug("Setting selection to Vagon Details Section.");
						vagonDetailSectionPart.setSelection(selection);
					}
				});

	}

	@Override
	public void setFocus() {
		selection = new VagonSelection();
		IVagonService vagonService = ServiceManager.getVagonService();
		try {
			List<VagonVO> listVagons = vagonService.getAllVagons();
			selection.setListVagons(listVagons);
			vagonTableSectionPart.setSelection(selection);
			vagonDetailSectionPart.setSelection(selection);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

}
